package com.ktg.mes.md.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ktg.mes.md.service.IMdItemService;
import com.ktg.common.constant.UserConstants;
import com.ktg.common.utils.StringUtils;
import com.ktg.mes.md.domain.MdItem;
import com.ktg.mes.md.mapper.MdItemMapper;
import com.ktg.mes.unit.sql.service.SQLToolService;
import com.ktg.mes.unit.sync.SyncModel;
import com.ktg.mes.unit.sync.service.IFromService;
import com.ktg.mes.unit.sync.service.IToService;
import com.ktg.mes.webSocket.NoticeWbsocketType;
import com.ktg.mes.webSocket.NoticeWebsocket;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class MdItemServiceImpl extends ServiceImpl<MdItemMapper, MdItem> implements IMdItemService {

    @Autowired
    private IFromService fromService;

    @Autowired
    private SQLToolService toIService;

    @Override
    public List<MdItem> selectMdItemList(MdItem mdItem) {
        return getBaseMapper().selectMdItemList(mdItem);
    }

    @Override
    public List<MdItem> selectMdItemList2(MdItem mdItem) {
        return getBaseMapper().selectMdItemList2(mdItem);
    }

    @Override
    public List<MdItem> selectMdItemStockList(MdItem mdItem) {
        return getBaseMapper().selectMdItemStockList(mdItem);
    }

    @Override
    public List<MdItem> selectMdItemAll() {
        return getBaseMapper().selectMdItemAll();
    }

    @Override
    public List<MdItem> selectMdItemBackUpAll() {
        return getBaseMapper().selectMdItemBackUpAll();
    }


    @Override
    public MdItem selectMdItemById(Long itemId) {
        return getBaseMapper().selectMdItemById(itemId);
    }

    @Override
    public MdItem selectMdItemByCode(String code) {
        return getBaseMapper().selectMdItemByCode(code);
    }

    @Override
    public String checkItemCodeUnique(MdItem mdItem) {
        MdItem item = getBaseMapper().checkItemCodeUnique(mdItem);
        Long itemId = mdItem.getItemId() == null ? -1L : mdItem.getItemId();
        if (StringUtils.isNotNull(item) && item.getItemId().longValue() != itemId.longValue()) {
            return UserConstants.NOT_UNIQUE;
        } else {
            return UserConstants.UNIQUE;
        }
    }

    @Override
    public String checkItemNameUnique(MdItem mdItem) {
        MdItem item = getBaseMapper().checkItemNameUnique(mdItem);
        Long itemId = mdItem.getItemId() == null ? -1L : mdItem.getItemId();
        if (StringUtils.isNotNull(item) && item.getItemId().longValue() != itemId.longValue()) {
            return UserConstants.NOT_UNIQUE;
        } else {
            return UserConstants.UNIQUE;
        }
    }


    /**
     * 同步库存
     */
    public synchronized void sysnStock() {
        NoticeWebsocket.sendMessage(NoticeWbsocketType.SyncT6, "开始同步产品信息");
        SyncModel syncModel = new SyncModel();
        syncModel.setService(fromService, toIService);

        String tableName = "Inventory";
        syncModel.setTable(tableName, "md_item");

        syncModel.setSyncData("cInvCode", "pubufts", "item_code", "pubufts");

        syncModel.addColumn("cInvCode", "item_code", SyncModel.SyncModelType.TypeString);
        syncModel.addColumn("cInvName", "item_name", SyncModel.SyncModelType.TypeString);
        syncModel.addColumn("cInvStd", "specification", SyncModel.SyncModelType.TypeString);
        syncModel.addColumn("cInvDefine1", "viscosity", SyncModel.SyncModelType.TypeString);
        syncModel.addColumn("cInvDefine2", "tag", SyncModel.SyncModelType.TypeString);
        syncModel.addColumn("cInvDefine3", "packing_type", SyncModel.SyncModelType.TypeString);
        syncModel.addColumn("cComUnitCode", "measure_no", SyncModel.SyncModelType.TypeString);
        syncModel.addColumn("cInvDefine5", "inv_box_std", SyncModel.SyncModelType.TypeString);
        syncModel.addColumn("cInvDefine4", "currency_name", SyncModel.SyncModelType.TypeString);

        syncModel.addColumn("cInvCCode", "item_type_Code", SyncModel.SyncModelType.TypeString);

        syncModel.addColumn("CONVERT(BIGINT,pubufts)", "pubufts", SyncModel.SyncModelType.TypeString);


        syncModel.putDefaultValue("safe_stock_flag", "N");
        syncModel.putDefaultValue("enable_flag", "Y");
        syncModel.putDefaultValue("unit_of_measure", "");
        syncModel.putDefaultValue("item_or_product", "");

        syncModel.synsStart("产品信息");
        //补全单位名称
        toIService.updateSql("UPDATE md_item,md_unit_measure set md_item.unit_of_measure = md_unit_measure.measure_name WHERE (md_item.unit_of_measure is null or md_item.unit_of_measure = '') AND md_item.measure_no = md_unit_measure.measure_code ;");
        //补齐T6分类名称和分类ID,和mes的产品分类
        toIService.updateSql("UPDATE md_item,md_item_type set md_item.item_type_id = md_item_type.item_type_id,md_item.item_type_name = md_item_type.item_type_name,md_item.item_or_product = IF(md_item.item_type_code like '03%','ITEM','PRODUCT')  \n" +
                "WHERE (md_item.item_type_name is null or md_item.item_type_name = '') AND md_item.item_type_code = md_item_type.item_type_code;");
        NoticeWebsocket.sendMessage(NoticeWbsocketType.SyncT6, "产品信息同步完成");

    }

    @Override
    public int deleteByItemIds(Long[] itemIds) {
        return getBaseMapper().deleteMdItemByIds(itemIds);
    }

//    @Override
//    public int deleteByItemId(Long itemId) {
//        return getBaseMapper().deleteMdItemById(itemId);
//    }

    @Override
    public String queryMaxPubfts() {
        return getBaseMapper().queryMaxPubfts();
    }


}
